<?php

namespace app\admin\controller;

/**
 *基本活动
 */
class LotteryBase extends base
{

    protected  $type,$db;

    public function _initialize()
    {
        $this->db = db('lottery');
    }

    /**
     * 列表
     */
    public function index()
    {
        $where['type'] = $this->type;
        $data = $this->db->where($where)->order('id DESC')->paginate();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 查看中奖情况
     */
    public function checkcode($id)
    {

        $getdata = $this->request->param();
        if (isset($getdata['islottery'])) {
            $where['islottery'] = $getdata['islottery'];
        }
        //只查看中奖的
        $where['islottery'] = '1';
        $where['lid'] = $id;

        $data = db('lottery_record')->order('id DESC')->where($where)->paginate();
        $count = db('lottery_record')->where($where)->count();

        $where['sendstutas'] = '1';
        $sendstutas = db('lottery_record')->where($where)->count();


        $this->assign([
            'data'=>$data,
            'count'=>$count,
            'sendstutas'=> $sendstutas
        ]);
        return $this->fetch('guajiang/checkcode');
    }

    /**
     * 添加活动
     * 保存
     */
    public function Add()
    {

        if ($this->request->isPost()) {
            $_POST['statdate'] = strtotime($this->request->param('statdate'));
            $_POST['enddate'] = strtotime($this->request->param('enddate'));
            $_POST['type'] = $this->type;
            if ($_POST['enddate'] < $_POST['statdate']) {
                ajaxReturn(['url' => url('')], 0, '结束时间不能小于开始时间');
            } else {
                if ($id = $this->db->insert($_POST)) {
                    ajaxReturn(['url' => url('index')], 1, '活动创建成功，请在列表中让活动“开始”');
                } else {
                    ajaxReturn(['url' => url('')], 0, '服务器繁忙,请稍候再试');
                }
            }
        } else {
            $now = time();
            $lottery['statdate'] = $now;
            $lottery['enddate'] = $now + 30 * 24 * 3600;
            $this->assign('vo', $lottery);
            return $this->fetch();
        }

    }

    /**
     * @param $id
     * @return mixed
     * 编辑活动
     */
    public function edit($id)
    {

        if (intval($id) < 1) {
            $this->error('数据错误');
        }
        $where['id'] = $id;

        if ($this->request->isPost()) {

            $data = db('Lottery');
            $_POST['id'] = $id;
            $_POST['token'] = session('token');
            $_POST['statdate'] = strtotime($_POST['statdate']);
            $_POST['enddate'] = strtotime($_POST['enddate']);
            if ($_POST['enddate'] < $_POST['statdate']) {
                ajaxReturn(['url' => url('', ['type' => '1'])], 0, '结束时间不能小于开始时间');
            } else {

                $check = $data->where($where)->find();
                if ($check == false) ajaxReturn(['url' => url('', ['type' => '1'])], 1, '非法操作');
                if ($data->where($where)->update($_POST)) {
                    ajaxReturn(['url' => url('index', ['type' => '1'])], 1, '修改成功');
                } else {
                    ajaxReturn(['url' => url('', ['type' => '1'])], 0, '没有修改！');
                }
            }
        } else {

            $data = db('Lottery');
            $lottery = $data->where($where)->find();

            if ($lottery == false) ajaxReturn(['url' => url('', ['type' => '1'])], 1, '非法操作');

            $this->assign('vo', $lottery);
            return $this->fetch('add');
        }

    }

    public function sendstutas()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if (isset($post['id']) && isset($post['status'])) {
                $msg = ($post['status']) ? '发奖成功' : '已经取消发奖';
                $data['sendstutas'] = $post['status'];
                $data['sendtime'] = ($post['status']) ? time() : '';
                $result = db('lottery_record')->where('id', $post['id'])->update($data);
                if ($result) {
                    ajaxMsg('1', $msg);
                }
                ajaxMsg('0', '操作失败');

            }
            ajaxMsg('0', '发奖失败');
        }

    }

    /**
     * 删除活动
     * @param $id 删除
     */
    public function delGuajiang($id)
    {
        if ($this->request->isAjax()) {
            $this->db->where('id', $id)->delete();
            db('lottery_record')->where('lid', $id)->delete();
            ajaxMsg('1', '删除成功');
        }
    }

    /**
     * 删除中奖情况
     * @param $id 删除
     */
    public function delrecord($id)
    {
        if ($this->request->isAjax()) {
            db('lottery_record')->where('id', $id)->delete();
            ajaxMsg('1', '删除成功');
        }
    }

    /**
     * 到处表格
     */
    public function exportSN()
    {
        header("Content-Type: text/html; charset=utf-8");
        header("Content-type:application/vnd.ms-execl");
        header("Content-Disposition:filename=huizong.xls");
        $letterArr = explode(',', strtoupper('a,b,c,d,e,f,g'));
        $arr = array(array('en' => 'sn', 'cn' => 'SN码(中奖号)'), array('en' => 'prize', 'cn' => '奖项'), array('en' => 'sendstutas', 'cn' => '是否已发奖品'), array('en' => 'sendtime', 'cn' => '奖品发送时间'), array('en' => 'phone', 'cn' => '中奖者手机号'), array('en' => 'wecha_name', 'cn' => '中奖者微信码'), array('en' => 'time', 'cn' => '中奖时间'),);
        $chengItem = array('piaomianjia', 'shuifei', 'yingshoujine', 'yingfupiaomianjia', 'yingfushuifei', 'yingfujine', 'dailishouru', 'fandian', 'jichangjianshefei', 'ranyoufei');
        $i = 0;
        $fieldCount = count($arr);
        $s = 0;
        foreach ($arr as $f) {
            if ($s < $fieldCount - 1) {
                echo iconv('utf-8', 'gbk', $f['cn']) . "\t";
            } else {
                echo iconv('utf-8', 'gbk', $f['cn']) . "\n";
            }
            $s++;
        }
        $db = db('Lottery_record');
        $sns = $db->where(array('lid' => $this->request->param('id'), 'islottery' => 1))->order('id ASC')->select();
        if ($sns) {

            foreach ($sns as $sn) {
                $j = 0;
                foreach ($arr as $field) {
                    $fieldValue = $sn[$field['en']];
                    switch ($field['en']) {
                        default:
                            break;
                        case 'time':
                        case 'sendtime':
                            if ($fieldValue) {
                                $fieldValue = date('Y-m-d H:i:s', $fieldValue);
                            } else {
                                $fieldValue = '';
                            }
                            break;
                        case 'wecha_name':
                        case 'prize':
                            $fieldValue = iconv('utf-8', 'gbk', $fieldValue);
                            break;
                    }
                    if ($j < $fieldCount - 1) {
                        echo $fieldValue . "\t";
                    } else {
                        echo $fieldValue . "\n";
                    }
                    $j++;
                }
                $i++;
            }
        }
        exit();
    }
    /**
     * 奖品显示名称
     * @param $id
     * @param $prizetype
     * @return mixed
     */
    static function getPrizeName($id, $prizetype)
    {
        $Lottery = cache('getPrizeName' . $id);
        if (empty($Lottery)) {
            $Lottery = db('Lottery')->where('id', $id)->field('fist,second,third,four,five,six')->find();
            cache('getPrizeName' . $id, $Lottery, 3600);
        }

        switch ($prizetype) {
            default:
                return $prizetype;
                break;
            case 1:
                return $Lottery['fist'];
                break;
            case 2:
                return $Lottery['second'];
                break;
            case 3:
                return $Lottery['third'];
                break;
            case 4:
                return $Lottery['four'];
                break;
            case 5:
                return $Lottery['five'];
                break;
            case 6:
                return $Lottery['six'];
                break;
        }
    }
}
